{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6b17ebba",
   "metadata": {},
   "source": [
    "### 第四周作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "64f81961",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import detect\n",
    "import analyze\n",
    "import face_compare\n",
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "355ee07e",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "\n",
    "face_token1 = 'b850ca0a7e3f476ad177c2bd64aa74c6'  # fike1\n",
    "face_token2 = 'eb93b50f33f9a4ec49ed7cab21d3f5d4'  # fike2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e7bc971a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648036279,9bef0d85-9bf7-49fc-83a8-c25fdb8d5ee6',\n",
       " 'time_used': 141,\n",
       " 'faces': [{'face_token': 'eb93b50f33f9a4ec49ed7cab21d3f5d4',\n",
       "   'face_rectangle': {'top': 114, 'left': 261, 'width': 200, 'height': 200}}],\n",
       " 'image_id': 'HtVYnJSFlMscqnJnDhYgMA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用人脸检测封装函数\n",
    "\n",
    "detect.face_detect(API_key,API_secret,'DominicFike2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30af9789",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 374,\n",
       " 'request_id': '1648036174,d77e9caf-f5dc-4075-af7b-d85f63653862',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 1050,\n",
       "     'x': 1138},\n",
       "    'mouth_upper_lip_top': {'y': 1009, 'x': 1217},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 1013, 'x': 1182},\n",
       "    'left_eye_upper_left_quarter': {'y': 766, 'x': 965},\n",
       "    'left_eyebrow_lower_middle': {'y': 687, 'x': 961},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 1062, 'x': 1165},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 707, 'x': 912},\n",
       "    'left_eye_bottom': {'y': 792, 'x': 1011},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 597, 'x': 1266},\n",
       "    'nose_contour_left1': {'y': 750, 'x': 1114},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 1045, 'x': 1302},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 1113, 'x': 1149},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 1114, 'x': 1194},\n",
       "    'mouth_lower_lip_bottom': {'y': 1103, 'x': 1245},\n",
       "    'contour_chin': {'y': 1290, 'x': 1293},\n",
       "    'contour_left9': {'y': 1298, 'x': 1206},\n",
       "    'mouth_lower_lip_top': {'y': 1064, 'x': 1233},\n",
       "    'right_eyebrow_upper_middle': {'y': 534, 'x': 1318},\n",
       "    'left_eyebrow_left_corner': {'y': 731, 'x': 868},\n",
       "    'contour_left6': {'y': 1180, 'x': 1012},\n",
       "    'right_eye_lower_right_quarter': {'y': 680, 'x': 1378},\n",
       "    'right_eye_bottom': {'y': 695, 'x': 1340},\n",
       "    'contour_left7': {'y': 1237, 'x': 1064},\n",
       "    'contour_right8': {'y': 1206, 'x': 1468},\n",
       "    'contour_left5': {'y': 1113, 'x': 972},\n",
       "    'contour_left4': {'y': 1042, 'x': 940},\n",
       "    'contour_left3': {'y': 971, 'x': 912},\n",
       "    'contour_left2': {'y': 899, 'x': 891},\n",
       "    'contour_left1': {'y': 825, 'x': 876},\n",
       "    'left_eye_lower_left_quarter': {'y': 798, 'x': 975},\n",
       "    'contour_right1': {'y': 632, 'x': 1562},\n",
       "    'right_eye_pupil': {'y': 672, 'x': 1331},\n",
       "    'contour_right2': {'y': 717, 'x': 1584},\n",
       "    'mouth_left_corner': {'y': 1101, 'x': 1110},\n",
       "    'contour_right4': {'y': 892, 'x': 1610},\n",
       "    'contour_right7': {'y': 1143, 'x': 1537},\n",
       "    'contour_left8': {'y': 1277, 'x': 1129},\n",
       "    'nose_right': {'y': 878, 'x': 1292},\n",
       "    'right_eye_upper_right_quarter': {'y': 650, 'x': 1370},\n",
       "    'nose_tip': {'y': 855, 'x': 1168},\n",
       "    'contour_right5': {'y': 982, 'x': 1609},\n",
       "    'nose_contour_lower_middle': {'y': 928, 'x': 1195},\n",
       "    'right_eye_top': {'y': 653, 'x': 1326},\n",
       "    'right_eye_right_corner': {'y': 660, 'x': 1412},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 993, 'x': 1248},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 1000, 'x': 1311},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 570, 'x': 1390},\n",
       "    'contour_right9': {'y': 1257, 'x': 1387},\n",
       "    'mouth_right_corner': {'y': 1026, 'x': 1369},\n",
       "    'right_eye_lower_left_quarter': {'y': 701, 'x': 1300},\n",
       "    'left_eyebrow_right_corner': {'y': 654, 'x': 1065},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 673, 'x': 1014},\n",
       "    'right_eye_center': {'y': 678, 'x': 1335},\n",
       "    'left_eye_pupil': {'y': 766, 'x': 1007},\n",
       "    'left_eye_upper_right_quarter': {'y': 744, 'x': 1038},\n",
       "    'nose_left': {'y': 922, 'x': 1101},\n",
       "    'right_eyebrow_lower_middle': {'y': 579, 'x': 1328},\n",
       "    'left_eye_center': {'y': 774, 'x': 1006},\n",
       "    'left_eye_lower_right_quarter': {'y': 777, 'x': 1044},\n",
       "    'right_eyebrow_left_corner': {'y': 610, 'x': 1202},\n",
       "    'right_eye_left_corner': {'y': 703, 'x': 1264},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 678, 'x': 896},\n",
       "    'left_eye_left_corner': {'y': 796, 'x': 943},\n",
       "    'left_eye_right_corner': {'y': 761, 'x': 1073},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 558, 'x': 1251},\n",
       "    'right_eyebrow_right_corner': {'y': 566, 'x': 1452},\n",
       "    'right_eye_upper_left_quarter': {'y': 672, 'x': 1288},\n",
       "    'left_eyebrow_upper_middle': {'y': 648, 'x': 947},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 532, 'x': 1389},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 1084, 'x': 1296},\n",
       "    'nose_contour_left2': {'y': 861, 'x': 1110},\n",
       "    'nose_contour_left3': {'y': 928, 'x': 1147},\n",
       "    'nose_contour_right1': {'y': 724, 'x': 1215},\n",
       "    'nose_contour_right2': {'y': 826, 'x': 1256},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 1059, 'x': 1337},\n",
       "    'mouth_upper_lip_bottom': {'y': 1040, 'x': 1226},\n",
       "    'contour_right6': {'y': 1068, 'x': 1585},\n",
       "    'nose_contour_right3': {'y': 906, 'x': 1245},\n",
       "    'left_eye_top': {'y': 746, 'x': 999},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 1025, 'x': 1295},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 637, 'x': 1007},\n",
       "    'contour_right3': {'y': 803, 'x': 1600},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 1083, 'x': 1170}},\n",
       "   'attributes': {'emotion': {'sadness': 0.068,\n",
       "     'neutral': 99.505,\n",
       "     'disgust': 0.039,\n",
       "     'anger': 0.008,\n",
       "     'surprise': 0.052,\n",
       "     'fear': 0.008,\n",
       "     'happiness': 0.32},\n",
       "    'beauty': {'female_score': 64.129, 'male_score': 56.971},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 22},\n",
       "    'mouthstatus': {'close': 100.0,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.0,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 5.258,\n",
       "     'stain': 48.809,\n",
       "     'acne': 32.823,\n",
       "     'health': 3.021},\n",
       "    'headpose': {'yaw_angle': 14.940365,\n",
       "     'pitch_angle': -4.693507,\n",
       "     'roll_angle': -14.182186},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.32},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.32},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.32}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.025},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.052,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.226,\n",
       "      'no_glass_eye_open': 99.152,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.57},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.021,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.025,\n",
       "      'no_glass_eye_open': 99.916,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.038}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 55.573},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.527,\n",
       "      'vector_z_component': 0.99,\n",
       "      'vector_x_component': 0.144,\n",
       "      'vector_y_component': -0.015,\n",
       "      'position_y_coordinate': 0.402},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.548,\n",
       "      'vector_z_component': 0.993,\n",
       "      'vector_x_component': 0.041,\n",
       "      'vector_y_component': 0.113,\n",
       "      'position_y_coordinate': 0.421}}},\n",
       "   'face_rectangle': {'width': 730, 'top': 548, 'left': 903, 'height': 730},\n",
       "   'face_token': 'b850ca0a7e3f476ad177c2bd64aa74c6'}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用人脸分析封装函数\n",
    "\n",
    "analyze.face_analyze(API_key,API_secret,face_token1,1,'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e8a2c13a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648036175,17ccfb4c-e143-48a3-9cdc-56b8da36aa4b',\n",
       " 'time_used': 1163,\n",
       " 'confidence': 84.325,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975},\n",
       " 'faces1': [{'face_token': 'ab0770c241f1d40401ca7bea7d181bce',\n",
       "   'face_rectangle': {'top': 114, 'left': 261, 'width': 200, 'height': 200}}],\n",
       " 'faces2': [{'face_token': '42f397f33321a512ad0873c13ed437f0',\n",
       "   'face_rectangle': {'top': 50, 'left': 268, 'width': 82, 'height': 82}}],\n",
       " 'image_id1': 'HtVYnJSFlMscqnJnDhYgMA==',\n",
       " 'image_id2': '4wp+Vom0OtGmB6PtBfQGqg=='}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用人脸比对封装函数\n",
    "\n",
    "face_compare.face_compare_url(API_key,API_secret,'https://tse1-mm.cn.bing.net/th/id/R-C.2bf40baf513ab4c59479fb329de81a55?rik=MaSEsRw0uXjO2A&riu=http%3a%2f%2fp2.music.126.net%2fOiQ-i7X5hQzJD8JlYebgXQ%3d%3d%2f109951165953551048.jpg&ehk=8mTm2xwmZrkLwTgu5S3IIesVAaXkxkCvJ03B63RRpDY%3d&risl=&pid=ImgRaw&r=0','https://tse3-mm.cn.bing.net/th/id/OIP-C.inGk1OtaZu1OwOs16Sz3MgHaE4?pid=ImgDet&rs=1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9e056052",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648036785,1b7f73d7-76bd-4e5c-8e8f-57908b3e1d8c',\n",
       " 'time_used': 745,\n",
       " 'confidence': 86.151,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare.face_compare_token(API_key,API_secret,face_token1,face_token2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5bc89e74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1648036873,3338e557-4884-460a-85ef-9c5c2ad2657b', 'time_used': 579, 'confidence': 86.151, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}\n"
     ]
    }
   ],
   "source": [
    "infoabout = face_compare.face_compare_token(API_key,API_secret,face_token1,face_token2)\n",
    "print(infoabout)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ee41a4db",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-13-88a89075658f>, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-13-88a89075658f>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m    'face_tokens':'b850ca0a7e3f476ad177c2bd64aa74c6'\u001b[0m\n\u001b[1;37m                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "library_add = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':'02f554d4181dcd149a55da71ab033614'\n",
    "    'face_tokens':'b850ca0a7e3f476ad177c2bd64aa74c6'\n",
    "}\n",
    "\n",
    "r = requests.post(library_info, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "23e4cda0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 75,\n",
       " 'error_message': 'EMPTY_FACESET',\n",
       " 'request_id': '1648551536,b1fad3ae-6e8c-4dfa-8268-fb14e2498918'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_key,API_secret,face_token1,'02f554d4181dcd149a55da71ab033614')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "27a70d3f",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "2",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-80689f4cee5e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mconfidence\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfoabout\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mthresholds3\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfoabout\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mthresholds4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfoabout\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mthresholds5\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfoabout\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 2"
     ]
    }
   ],
   "source": [
    "# confidence = infoabout[2]\n",
    "# thresholds3 = infoabout[3][0]\n",
    "# thresholds4 = infoabout[3][1]\n",
    "# thresholds5 = infoabout[3][2]\n",
    "\n",
    "# if confidence > thresholds5:\n",
    "#     print(\"这两张图片大概率是同一个人！\")\n",
    "# elif confidence < thresholds3:\n",
    "#     print(\"这两张图片不太可能是同一个人...\")\n",
    "# else:\n",
    "#     print(\"啊，天哪我也不太确定\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8ccc428",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
